﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExchangeListener
{
    class ContactSync
    {
        private ExchangeServiceBinding esb;

        public ContactSync(ExchangeServiceBinding _esb)
        {
            this.esb = _esb;
        }

        public void DoWork()
        {
            ContactItemType[] cits = ExchangeContacts.Instance.GetExchangeContactItems(esb);

            GatewayContactItemType[] gcits = GatewayContacts.Instance.GetGatewayContactItemType();

            Compare(cits, gcits);
        }

        private void Compare(ContactItemType[] cits, GatewayContactItemType[] gcits)
        {
            foreach (var item in gcits)
            {
                var contacts = cits.Where(c => c.DisplayName.Equals(item.Name));
                if (contacts != null)
                {
                    if (contacts.Count() > 0)
                    {
                        foreach (ContactItemType contact in contacts)
                        {
                            if (contact.LastModifiedTime < item.LastModifiedTime)
                            {
                                int i = 0;
                                foreach (var mobile in contact.PhoneNumbers)
                                {
                                    if (mobile.Key == PhoneNumberKeyType.MobilePhone)
                                    {
                                        mobile.Value = item.Moblie;
                                        i = 1;
                                        ExchangeContacts.Instance.UpdateExchangeContact(contact, esb);
                                        break;
                                    }
                                }
                                if (i == 0)
                                {                                    
                                    var list = contact.PhoneNumbers.ToList();
                                    PhoneNumberDictionaryEntryType pnde = new PhoneNumberDictionaryEntryType();
                                    pnde.Key = PhoneNumberKeyType.MobilePhone;
                                    pnde.Value = item.Moblie;
                                    list.Add(pnde);
                                    contact.PhoneNumbers = list.ToArray();
                                    ExchangeContacts.Instance.AddMobilePhoneNumber(contact, esb);
                                }
                            }
                        }
                    }
                    else
                    {
                        ContactItemType newcontact = new ContactItemType();
                        newcontact.DisplayName = item.Name;
                        newcontact.Surname = item.Name;
                        PhoneNumberDictionaryEntryType pnde = new PhoneNumberDictionaryEntryType();
                        pnde.Key = PhoneNumberKeyType.MobilePhone;
                        pnde.Value = item.Moblie;
                        PhoneNumberDictionaryEntryType[] ps = new PhoneNumberDictionaryEntryType[1];
                        ps[0] = pnde;
                        newcontact.PhoneNumbers = ps;
                        ExchangeContacts.Instance.CreateNewExchangeContact(newcontact, esb);
                    }
                }
            }
        }
    }
}
